<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>libdill</title>
  <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>

<h1>libdill: Structured Concurrency for C</h1>

<ul id='toplist'>
<li><a href="index.html">Home</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorials</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
<h1 id="name">NAME</h1>
<p>term_detach - cleanly terminates the protocol</p>
<h1 id="synopsis">SYNOPSIS</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="pp">#include </span><span class="im">&lt;libdill.h&gt;</span>

<span class="dt">int</span> term_detach(
    <span class="dt">int</span> s,
    <span class="dt">int64_t</span> deadline);</code></pre></div>
<h1 id="description">DESCRIPTION</h1>
<p>TERM is a protocol that implements clean terminal handshake between peers. When creating the protocol instance user specifies the terminal message to use. When closing the protocol, terminal messages are exchanged between peers in both directions. After the protocol shuts down the peers agree on their position in the message stream.</p>
<p>This function cleanly terminates TERM protocol. If termination message wasn't yet sent to the peer using <strong>term_done</strong> function it will be sent now. Afterwards, any outstanding inbound messages will be received and dropped silently. The function returns after termination message from the peer is received.</p>
<p><strong>s</strong>: The TERM protocol handle.</p>
<p><strong>deadline</strong>: A point in time when the operation should time out, in milliseconds. Use the <strong>now</strong> function to get your current point in time. 0 means immediate timeout, i.e., perform the operation if possible or return without blocking if not. -1 means no deadline, i.e., the call will block forever if the operation cannot be performed.</p>
<p>This function is not available if libdill is compiled with <strong>--disable-sockets</strong> option.</p>
<h1 id="return-value">RETURN VALUE</h1>
<p>In case of success the function returns 0. In case of error it returns -1 and sets <strong>errno</strong> to one of the values below.</p>
<h1 id="errors">ERRORS</h1>
<ul>
<li><strong>EBADF</strong>: Invalid handle.</li>
<li><strong>ECANCELED</strong>: Current coroutine was canceled.</li>
<li><strong>ENOTSUP</strong>: The handle is not a TERM protocol handle.</li>
<li><strong>ETIMEDOUT</strong>: Deadline was reached.</li>
</ul>
<h1 id="example">EXAMPLE</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c">s = term_attach(s, <span class="st">&quot;STOP&quot;</span>, <span class="dv">4</span>);
...
<span class="co">/* Send terminal message to the peer. */</span>
term_done(s, <span class="dv">-1</span>);
<span class="co">/* Process remaining inbound messages. */</span>
<span class="cf">while</span>(<span class="dv">1</span>) {
    <span class="dt">char</span> buf[<span class="dv">256</span>];
    <span class="dt">ssize_t</span> sz = mrecv(s, buf, <span class="kw">sizeof</span>(buf), <span class="dv">-1</span>);
    <span class="co">/* Check whether terminal message was received from the peer. */</span>
    <span class="cf">if</span>(sz &lt; <span class="dv">0</span> &amp;&amp; errno == EPIPE) <span class="cf">break</span>;
    frobnicate(buff, sz);
}
s = term_detach(s);</code></pre></div>
<h1 id="see-also">SEE ALSO</h1>
<p><strong>mrecv</strong>(3) <strong>mrecvl</strong>(3) <strong>msend</strong>(3) <strong>msendl</strong>(3) <strong>now</strong>(3) <strong>term_attach</strong>(3) <strong>term_attach_mem</strong>(3) <strong>term_done</strong>(3)</p>
</body>
